

struct ListNode
{
    int val;
    ListNode *next;
    ListNode(int x) : val(x), next(nullptr) {}
};

class Solution
{
public:
    bool hasCycle(ListNode *head)
    {
        if (!head) return false;
        ListNode* fastptr = head;
        ListNode* slowptr = head;
        while (fastptr && slowptr) {
            fastptr = fastptr->next;
            if (fastptr) fastptr = fastptr->next;
            slowptr = slowptr->next;
            if (fastptr && fastptr == slowptr) return true;
        }
        return false;

    }
};